package rank

import (
	"sort"
)

// GetIncomeRank 获取收入排行榜
func GetIncomeRank(page, pageSize int) ([]IncomeRank, error) {
	randDao := NewRankDao()
	// 构建收入排行榜
	var incomeRank = randDao.getIncomeRank()
	// 按收入排序
	sort.Slice(incomeRank, func(i, j int) bool {
		return incomeRank[i].Income > incomeRank[j].Income
	})
	// 分页处理
	startIndex := (page - 1) * pageSize
	endIndex := startIndex + pageSize
	if startIndex > len(incomeRank) {
		return []IncomeRank{}, nil
	}
	if endIndex > len(incomeRank) {
		endIndex = len(incomeRank)
	}
	return incomeRank[startIndex:endIndex], nil
}

// GetAbilityRank 获取能力排行榜
func GetAbilityRank(page, pageSize int) ([]AbilityRank, error) {
	randDao := NewRankDao()
	// 构建能力排行榜
	var abilityRank = randDao.getAbilityRank()

	// 按能力值排序
	sort.Slice(abilityRank, func(i, j int) bool {
		return abilityRank[i].Ability > abilityRank[j].Ability
	})

	// 分页处理
	startIndex := (page - 1) * pageSize
	endIndex := startIndex + pageSize
	if startIndex > len(abilityRank) {
		return []AbilityRank{}, nil
	}
	if endIndex > len(abilityRank) {
		endIndex = len(abilityRank)
	}
	return abilityRank[startIndex:endIndex], nil
}

// GetCharmRank 获取魅力排行榜
func GetCharmRank(page, pageSize int) ([]CharmRank, error) {
	rankDao := NewRankDao()
	// 构建魅力排行榜
	var charmRank = rankDao.getCharmRank()

	// 按魅力值排序
	sort.Slice(charmRank, func(i, j int) bool {
		return charmRank[i].Charm > charmRank[j].Charm
	})

	// 分页处理
	startIndex := (page - 1) * pageSize
	endIndex := startIndex + pageSize
	if startIndex > len(charmRank) {
		return []CharmRank{}, nil
	}
	if endIndex > len(charmRank) {
		endIndex = len(charmRank)
	}
	return charmRank[startIndex:endIndex], nil
}

// GetGuildRank 获取公会排行榜
func GetGuildRank(page, pageSize int) ([]GuildRank, error) {
	rankDao := NewRankDao()

	var guildRank = rankDao.getGuildRank()

	sort.Slice(guildRank, func(i, j int) bool {
		return guildRank[i].Income > guildRank[j].Income
	})
	// 分页处理
	startIndex := (page - 1) * pageSize
	endIndex := startIndex + pageSize
	if startIndex > len(guildRank) {
		return []GuildRank{}, nil
	}
	if endIndex > len(guildRank) {
		endIndex = len(guildRank)
	}
	return guildRank[startIndex:endIndex], nil
}

func init() {
	randDao := NewRankDao()
	randDao.Inster()
}
